package test4.B2;

import java.time.LocalDateTime;
import java.time.Duration;
import java.util.Scanner;

enum ControlPanelState {
    WAITING_FOR_INPUT,  // 等待输入密码
    VERIFYING_PASSWORD, // 正在验证密码
    LOCKED,             // 控制面板已锁定
    SYSTEM_MENU         // 系统功能选择状态
}

public class ControlPanel {
    private static final String CORRECT_PASSWORD = "1234"; // 预设正确密码
    private static final int MAX_ATTEMPTS = 3;             // 最大尝试次数
    private static final int LOCK_DURATION_SECONDS = 120;  // 锁定时长（秒）

    private ControlPanelState currentState;  // 当前状态
    private StringBuilder enteredPassword;   // 用户输入的密码
    private int failedAttempts;              // 失败的尝试次数
    private LocalDateTime lockStartTime;     // 锁定开始时间

    public ControlPanel() {
        this.currentState = ControlPanelState.WAITING_FOR_INPUT;
        this.enteredPassword = new StringBuilder();
        this.failedAttempts = 0;
        this.lockStartTime = null;
    }

    // 用户输入字符
    public void enterCharacter(char c) {
        switch (currentState) {
            case WAITING_FOR_INPUT:
                enteredPassword.append(c);
                System.out.println("输入密码字符: " + c);

                // 检查密码长度是否已达到4位
                if (enteredPassword.length() == CORRECT_PASSWORD.length()) {
                    currentState = ControlPanelState.VERIFYING_PASSWORD;
                    verifyPassword();
                }
                break;

            case LOCKED:
                if (isLockExpired()) {
                    currentState = ControlPanelState.WAITING_FOR_INPUT;
                    failedAttempts = 0;
                    enteredPassword.setLength(0);
                    System.out.println("锁定已解除，可以重新输入密码。");
                } else {
                    System.out.println("控制面板已锁定，请稍后再试。");
                }
                break;

            default:
                System.out.println("当前无法输入字符。");
                break;
        }
    }

    // 验证密码
    private void verifyPassword() {
        if (enteredPassword.toString().equals(CORRECT_PASSWORD)) {
            System.out.println("密码正确，进入系统功能选择状态。");
            currentState = ControlPanelState.SYSTEM_MENU;
        } else {
            System.out.println("密码错误！");
            failedAttempts++;
            enteredPassword.setLength(0); // 清除输入的密码

            if (failedAttempts >= MAX_ATTEMPTS) {
                currentState = ControlPanelState.LOCKED;
                lockStartTime = LocalDateTime.now();
                System.out.println("尝试次数过多，控制面板已锁定。");
            } else {
                currentState = ControlPanelState.WAITING_FOR_INPUT;
            }
        }
    }

    // 检查锁定时间是否已过
    private boolean isLockExpired() {
        if (lockStartTime == null) {
            return true;
        }
        Duration lockDuration = Duration.between(lockStartTime, LocalDateTime.now());
        return lockDuration.getSeconds() >= LOCK_DURATION_SECONDS;
    }

    // 返回到等待输入密码状态
    public void resetToPasswordInput() {
        if (currentState == ControlPanelState.SYSTEM_MENU) {
            currentState = ControlPanelState.WAITING_FOR_INPUT;
            enteredPassword.setLength(0);
            System.out.println("返回到密码输入状态。");
        }
    }

    // 获取当前状态
    public ControlPanelState getCurrentState() {
        return currentState;
    }

    public static void main(String[] args) {
        ControlPanel controlPanel = new ControlPanel();
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入密码：");

        while (true) {
            String input = scanner.nextLine();
            if (input.length() == 1) {
                controlPanel.enterCharacter(input.charAt(0));
            } else {
                System.out.println("请输入单个字符。");
            }

            // 输出当前状态
            System.out.println("当前状态: " + controlPanel.getCurrentState());

            // 退出条件
            if (controlPanel.getCurrentState() == ControlPanelState.SYSTEM_MENU) {
                System.out.println("已进入系统功能选择状态。");
                break;
            }
        }
        scanner.close();
    }
}

